home *** CD-ROM | disk | FTP | other *** search
/ Ham Radio 2000 #1 / Ham Radio 2000.iso / ham2000 / tcp_ip / wnos / wn941101 / sockutil.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-10  |  2.8 KB  |  145 lines

  1. #include "global.h"
  2. #include "mbuf.h"
  3. #include "netuser.h"
  4. #include "socket.h"
  5. #include "usock.h"
  6. #include "ax25.h"
  7. #include "tcp.h"
  8. #include "nr4.h"
  9. #include "config.h"
  10.  
  11. /* Convert a socket (address + port) to an ascii string of the form
  12.  * aaa.aaa.aaa.aaa:ppppp
  13.  */
  14. char *
  15. psocket(p)
  16. void *p;    /* Pointer to structure to decode */
  17. {
  18. #if    defined(AX25) || defined(NETROM)
  19.     char tmp[AXBUF];
  20. #endif
  21.     static char buf[30];
  22.     union sp sp;
  23.     struct socket socket;
  24.  
  25.     sp.p = p;
  26.  
  27.     *buf = '\0';
  28.  
  29.     switch(sp.sa->sa_family){
  30.     case AF_LOCAL:
  31. /* DL8YQ */        buf[0] = '\0';
  32.         break;
  33.     case AF_INET:
  34.         socket.address = sp.in->sin_addr.s_addr;
  35.         socket.port = sp.in->sin_port;
  36.         strcpy(buf,pinet(&socket));
  37.         break;
  38. #ifdef    AX25
  39.     case AF_AX25:
  40.         pax25(tmp,sp.ax->ax25_addr);
  41.         if(strlen(sp.ax->iface) != 0)
  42.             sprintf(buf,"%s on %s",tmp,sp.ax->iface);
  43.         else
  44.             strcpy(buf,tmp);
  45.         break;
  46. #endif
  47. #ifdef    NETROM
  48.     case AF_NETROM:
  49.         pax25(tmp,sp.nr->nr_addr.user);
  50.         sprintf(buf,"%s @ ",tmp);
  51.         pax25(tmp,sp.nr->nr_addr.node);
  52.         strcat(buf,tmp);
  53.         break;
  54. #endif
  55.     }
  56.     return buf;
  57. }
  58.  
  59. /* Return ASCII string giving reason for connection closing */
  60. char *
  61. sockerr(int s)        /* Socket index */
  62. {
  63.     register struct usock *up;
  64.  
  65.     if((up = itop(s)) == NULLUSOCK){
  66.         errno = EBADF;
  67.         return Badsocket;
  68.     }
  69.     switch(up->type){
  70. #ifdef    AX25
  71.     case TYPE_AX25I:
  72.         if(up->cb.ax25 != NULLAX25)
  73.             return NULLCHAR;    /* nothing wrong */
  74.         return Ax25reasons[up->errcodes[0]];
  75. #endif
  76. #ifdef    NETROM
  77.     case TYPE_NETROML4:
  78.         if(up->cb.nr4 != NULLNR4CB)
  79.             return NULLCHAR;    /* nothing wrong */
  80.         return Nr4reasons[up->errcodes[0]];
  81. #endif
  82.     case TYPE_TCP:
  83.         if(up->cb.tcb != NULLTCB)
  84.             return NULLCHAR;    /* nothing wrong */
  85.         return Tcpreasons[up->errcodes[0]];
  86.     default:
  87.         errno = EOPNOTSUPP;    /* not yet, anyway */
  88.         return NULLCHAR;
  89.     }
  90. }
  91.  
  92. /* Get state of protocol. Valid only for connection-oriented sockets. */
  93. char *
  94. sockstate(s)
  95. int s;        /* Socke+t index */
  96. {
  97.     register struct usock *up;
  98.  
  99.     if((up = itop(s)) == NULLUSOCK){
  100.         errno = EBADF;
  101.         return NULLCHAR;
  102.     }
  103.     if(up->cb.p == NULLCHAR){
  104.         errno = ENOTCONN;
  105.         return NULLCHAR;
  106.     }
  107.     switch(up->type){
  108.     case TYPE_TCP:
  109.         return Tcpstates[up->cb.tcb->state];
  110. #ifdef    AX25
  111.     case TYPE_AX25I:
  112.         return Ax25states[up->cb.ax25->state];
  113. #endif
  114. #ifdef    NETROM
  115.     case TYPE_NETROML4:
  116.         return Nr4states[up->cb.nr4->state];
  117. #endif
  118.     default:
  119.         /* Datagram sockets don't have state */
  120.         errno = EOPNOTSUPP;
  121.         return NULLCHAR;
  122.     }
  123.     /*NOTREACHED*/
  124. }
  125.  
  126. /* Convert a socket index to an internal user socket structure pointer */
  127. struct usock *
  128. itop(s)
  129. register int s;                    /* Socket index */
  130.  
  131. {
  132.     register struct usock *up;
  133.     s -= SOCKBASE;
  134.  
  135.     if(s < 0 || s >= Nusock)
  136.         return NULLUSOCK;
  137.  
  138.     up = &Usock[s];
  139.  
  140.     if(up->type == NOTUSED)
  141.         return NULLUSOCK;
  142.     return up;
  143. }
  144.  
  145.